using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZedGraph;

namespace ICP_MS.Plugin.Advance;

public class FormSemiConductor : Form
{
	private List<DateTime> dt = new List<DateTime>();

	private List<double> temperature = new List<double>();

	private IContainer components = null;

	private Panel panel1;

	private Button btnSet;

	private TextBox textBox1;

	private ZedGraphControl graph;

	private GroupBox groupBox1;

	private System.Windows.Forms.Label label1;

	private Button btnSave;

	private GroupBox groupBox2;

	private TextBox txtBackTemperature;

	private System.Windows.Forms.Label label5;

	private System.Windows.Forms.Label label3;

	private System.Windows.Forms.Label label4;

	private TextBox txtRealTemprature;

	private System.Windows.Forms.Label label2;

	public FormSemiConductor()
	{
		InitializeComponent();
	}

	private void btnSet_Click(object sender, EventArgs e)
	{
		try
		{
			float xx = 0f;
			xx = float.Parse(textBox1.Text);
			SemiConductor.Instance.SetVoltage(xx);
			MessageBox.Show("Success!");
		}
		catch (Exception ex)
		{
			MessageBox.Show(ex.Message, Lang.Txt("AddFormIonSource.Warn"), MessageBoxButtons.OK);
		}
	}

	private void FormSemiConductor_Load(object sender, EventArgs e)
	{
		graph.GraphPane.Title.Text = Lang.Txt("ADD.SemiConductor.TempCurve");
		graph.GraphPane.XAxis.Title.Text = Lang.Txt("ADD.SemiConductor.Time");
		graph.GraphPane.YAxis.Title.Text = Lang.Txt("ADD.SemiConductor.Temp");
		graph.GraphPane.XAxis.Type = AxisType.Date;
		graph.GraphPane.XAxis.Scale.Format = "HH:mm:ss";
		graph.GraphPane.Y2Axis.IsVisible = true;
		graph.GraphPane.AddCurve(Lang.Txt("ADD.SemiConductor.Temp"), new PointPairList(), Color.Blue, SymbolType.None);
		ApplyResources();
		SemiConductor.DataReceiveEvent = (EventHandler<SemiEventArgs>)Delegate.Combine(SemiConductor.DataReceiveEvent, new EventHandler<SemiEventArgs>(DataReceive));
	}

	private void ApplyResources()
	{
		groupBox1.Text = Lang.Txt("ADD.SemiConductor.TempSet");
		label1.Text = Lang.Txt("ADD.SemiConductor.Temp");
		btnSet.Text = Lang.Txt("ADD.SemiConductor.Set");
		groupBox2.Text = Lang.Txt("ADD.SemiConductor.TempShow");
		label2.Text = Lang.Txt("ADD.SemiConductor.BackplaneTemp");
		label4.Text = Lang.Txt("ADD.SemiConductor.RefrigerationTemp");
		graph.GraphPane.Title.Text = Lang.Txt("ADD.SemiConductor.TempCurve");
		graph.GraphPane.XAxis.Title.Text = Lang.Txt("ADD.SemiConductor.Time");
		graph.GraphPane.YAxis.Title.Text = Lang.Txt("ADD.SemiConductor.Temp");
	}

	private void DataReceive(object sender, SemiEventArgs e)
	{
		switch (e.index)
		{
		case 1:
		{
			DateTime time = DateTime.Now;
			dt.Add(time);
			temperature.Add(e.ss);
			graph.GraphPane.CurveList[0].AddPoint(new XDate(time), e.ss);
			if (graph.GraphPane.CurveList[0].Points.Count > 20000)
			{
				graph.GraphPane.CurveList[0].RemovePoint(0);
			}
			Invoke((MethodInvoker)delegate
			{
				txtRealTemprature.Text = e.ss.ToString("F2");
				graph.Refresh();
				graph.AxisChange();
			});
			break;
		}
		case 2:
			Invoke((MethodInvoker)delegate
			{
				txtBackTemperature.Text = e.ss.ToString("F2");
			});
			break;
		}
	}

	private void FormSemiConductor_FormClosing(object sender, FormClosingEventArgs e)
	{
		SemiConductor.DataReceiveEvent = (EventHandler<SemiEventArgs>)Delegate.Remove(SemiConductor.DataReceiveEvent, new EventHandler<SemiEventArgs>(DataReceive));
	}

	private void btnSave_Click(object sender, EventArgs e)
	{
		Task.Factory.StartNew(delegate
		{
			string path = Application.StartupPath + "\\TestData\\SemiConductor.csv";
			using StreamWriter streamWriter = new StreamWriter(path);
			try
			{
				for (int i = 0; i < dt.Count; i++)
				{
					streamWriter.Write(dt[i]);
					streamWriter.Write(",");
					streamWriter.Write(temperature[i]);
					streamWriter.WriteLine();
				}
				MessageBox.Show("Success！", "Info");
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, Lang.Txt("AddFormIonSource.Warn"), MessageBoxButtons.OK);
			}
		});
	}

	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	private void InitializeComponent()
	{
		this.components = new System.ComponentModel.Container();
		this.panel1 = new System.Windows.Forms.Panel();
		this.groupBox2 = new System.Windows.Forms.GroupBox();
		this.txtBackTemperature = new System.Windows.Forms.TextBox();
		this.graph = new ZedGraph.ZedGraphControl();
		this.groupBox1 = new System.Windows.Forms.GroupBox();
		this.btnSave = new System.Windows.Forms.Button();
		this.label1 = new System.Windows.Forms.Label();
		this.btnSet = new System.Windows.Forms.Button();
		this.textBox1 = new System.Windows.Forms.TextBox();
		this.label2 = new System.Windows.Forms.Label();
		this.label3 = new System.Windows.Forms.Label();
		this.txtRealTemprature = new System.Windows.Forms.TextBox();
		this.label4 = new System.Windows.Forms.Label();
		this.label5 = new System.Windows.Forms.Label();
		this.panel1.SuspendLayout();
		this.groupBox2.SuspendLayout();
		this.groupBox1.SuspendLayout();
		base.SuspendLayout();
		this.panel1.AutoSize = true;
		this.panel1.BackColor = System.Drawing.Color.WhiteSmoke;
		this.panel1.Controls.Add(this.groupBox2);
		this.panel1.Controls.Add(this.graph);
		this.panel1.Controls.Add(this.groupBox1);
		this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
		this.panel1.Location = new System.Drawing.Point(0, 0);
		this.panel1.Name = "panel1";
		this.panel1.Size = new System.Drawing.Size(1001, 554);
		this.panel1.TabIndex = 1;
		this.groupBox2.Controls.Add(this.label5);
		this.groupBox2.Controls.Add(this.label3);
		this.groupBox2.Controls.Add(this.label4);
		this.groupBox2.Controls.Add(this.txtRealTemprature);
		this.groupBox2.Controls.Add(this.label2);
		this.groupBox2.Controls.Add(this.txtBackTemperature);
		this.groupBox2.Location = new System.Drawing.Point(255, 354);
		this.groupBox2.Name = "groupBox2";
		this.groupBox2.Size = new System.Drawing.Size(248, 91);
		this.groupBox2.TabIndex = 70;
		this.groupBox2.TabStop = false;
		this.groupBox2.Text = "温度显示";
		this.txtBackTemperature.ForeColor = System.Drawing.Color.Blue;
		this.txtBackTemperature.Location = new System.Drawing.Point(89, 21);
		this.txtBackTemperature.Name = "txtBackTemperature";
		this.txtBackTemperature.ReadOnly = true;
		this.txtBackTemperature.Size = new System.Drawing.Size(105, 21);
		this.txtBackTemperature.TabIndex = 0;
		this.txtBackTemperature.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
		this.graph.Location = new System.Drawing.Point(3, 3);
		this.graph.Name = "graph";
		this.graph.ScrollGrace = 0.0;
		this.graph.ScrollMaxX = 0.0;
		this.graph.ScrollMaxY = 0.0;
		this.graph.ScrollMaxY2 = 0.0;
		this.graph.ScrollMinX = 0.0;
		this.graph.ScrollMinY = 0.0;
		this.graph.ScrollMinY2 = 0.0;
		this.graph.Size = new System.Drawing.Size(992, 343);
		this.graph.TabIndex = 68;
		this.groupBox1.Controls.Add(this.btnSave);
		this.groupBox1.Controls.Add(this.label1);
		this.groupBox1.Controls.Add(this.btnSet);
		this.groupBox1.Controls.Add(this.textBox1);
		this.groupBox1.Location = new System.Drawing.Point(3, 352);
		this.groupBox1.Name = "groupBox1";
		this.groupBox1.Size = new System.Drawing.Size(246, 93);
		this.groupBox1.TabIndex = 69;
		this.groupBox1.TabStop = false;
		this.groupBox1.Text = "温度设置 ";
		this.btnSave.Location = new System.Drawing.Point(9, 53);
		this.btnSave.Name = "btnSave";
		this.btnSave.Size = new System.Drawing.Size(85, 27);
		this.btnSave.TabIndex = 2;
		this.btnSave.Text = "保存";
		this.btnSave.UseVisualStyleBackColor = true;
		this.btnSave.Visible = false;
		this.btnSave.Click += new System.EventHandler(btnSave_Click);
		this.label1.Location = new System.Drawing.Point(23, 20);
		this.label1.Name = "label1";
		this.label1.Size = new System.Drawing.Size(61, 22);
		this.label1.TabIndex = 1;
		this.label1.Text = "温度：";
		this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
		this.btnSet.Location = new System.Drawing.Point(144, 52);
		this.btnSet.Name = "btnSet";
		this.btnSet.Size = new System.Drawing.Size(82, 27);
		this.btnSet.TabIndex = 1;
		this.btnSet.Text = "设置";
		this.btnSet.UseVisualStyleBackColor = true;
		this.btnSet.Click += new System.EventHandler(btnSet_Click);
		this.textBox1.Location = new System.Drawing.Point(90, 20);
		this.textBox1.Name = "textBox1";
		this.textBox1.Size = new System.Drawing.Size(136, 21);
		this.textBox1.TabIndex = 0;
		this.label2.Location = new System.Drawing.Point(10, 24);
		this.label2.Name = "label2";
		this.label2.Size = new System.Drawing.Size(70, 17);
		this.label2.TabIndex = 1;
		this.label2.Text = "背板温度：";
		this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
		this.label3.AutoSize = true;
		this.label3.Location = new System.Drawing.Point(200, 29);
		this.label3.Name = "label3";
		this.label3.Size = new System.Drawing.Size(17, 12);
		this.label3.TabIndex = 2;
		this.label3.Text = "℃";
		this.txtRealTemprature.ForeColor = System.Drawing.Color.Blue;
		this.txtRealTemprature.Location = new System.Drawing.Point(89, 57);
		this.txtRealTemprature.Name = "txtRealTemprature";
		this.txtRealTemprature.ReadOnly = true;
		this.txtRealTemprature.Size = new System.Drawing.Size(105, 21);
		this.txtRealTemprature.TabIndex = 0;
		this.txtRealTemprature.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
		this.label4.Location = new System.Drawing.Point(10, 60);
		this.label4.Name = "label4";
		this.label4.Size = new System.Drawing.Size(70, 17);
		this.label4.TabIndex = 1;
		this.label4.Text = "制冷温度：";
		this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
		this.label5.AutoSize = true;
		this.label5.Location = new System.Drawing.Point(200, 65);
		this.label5.Name = "label5";
		this.label5.Size = new System.Drawing.Size(17, 12);
		this.label5.TabIndex = 2;
		this.label5.Text = "℃";
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 12f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.ClientSize = new System.Drawing.Size(1001, 554);
		base.Controls.Add(this.panel1);
		base.Name = "FormSemiConductor";
		this.Text = "半导体制冷";
		base.FormClosing += new System.Windows.Forms.FormClosingEventHandler(FormSemiConductor_FormClosing);
		base.Load += new System.EventHandler(FormSemiConductor_Load);
		this.panel1.ResumeLayout(false);
		this.groupBox2.ResumeLayout(false);
		this.groupBox2.PerformLayout();
		this.groupBox1.ResumeLayout(false);
		this.groupBox1.PerformLayout();
		base.ResumeLayout(false);
		base.PerformLayout();
	}
}
